﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace MODULE_DANHMUC
{
    public partial class frm_danhmucnguoisudung : Form
    {
        danhmucDataContext context = cls_danhmuc.getcontext();
        public frm_danhmucnguoisudung()
        {
            InitializeComponent();
        }

        private void frm_danhmucnguoisudung_Load(object sender, EventArgs e)
        {


            this.chkduocthem.Checked = true;
            this.chkduocsuatutao.Checked = true;
            this.chkduocxoatutao.Checked = true;
            getlistnguoisudung();
            string manv = this.lstdanhsach.SelectedValue.ToString();
            getdata(manv);

        }

        private void getlistnguoisudung()
        {

            this.lstdanhsach.DataSource = context.DM_nhanviens;
            this.lstdanhsach.DisplayMember = "hoten";
            this.lstdanhsach.ValueMember = "manv";
        }

        private void lstdanhsach_Click(object sender, EventArgs e)
        {

        }

        private void lstdanhsach_SelectedIndexChanged(object sender, EventArgs e)
        {

            string manv = this.lstdanhsach.SelectedValue.ToString();
            getdata(manv);
            //MessageBox.Show(manv);

        }
        private void getdata(string manv)
        {
            this.grdata.Rows.Clear();

            IEnumerable<DM_nhanvien> l = from c in context.DM_nhanviens where c.manv.Equals(manv) select c;
            if (l.Count() > 0)
            {
                DM_nhanvien nv = l.ElementAt(0);
                this.txttennsd.Text = nv.hoten;
                this.txtusername.Text = nv.username;

                this.txtpassword.Text = nv.password;
                if (nv.quyenthem == null)
                {
                    this.chkduocthem.Checked = false;
                    this.chkkhongthem.Checked = false;
                }
                if (nv.quyenthem == 0)
                {
                    this.chkkhongthem.Checked = true;
                    this.chkduocthem.Checked = false;
                }
                if (nv.quyenthem == 1)
                {
                    this.chkkhongthem.Checked = false;
                    this.chkduocthem.Checked = true;
                }

                ////////////////////////////////
                if (nv.quyensua == null)
                {
                    chkkhongsua.Checked = false;
                    chkduocsuatutao.Checked = false;
                    chkduocsua.Checked = false;
                }
                if (nv.quyensua == 0)
                {
                    chkkhongsua.Checked = true;
                    chkduocsuatutao.Checked = false;
                    chkduocsua.Checked = false;
                }
                if (nv.quyensua == 1)
                {
                    chkkhongsua.Checked = false;
                    chkduocsuatutao.Checked = true;
                    chkduocsua.Checked = false;
                }
                if (nv.quyensua == 2)
                {
                    chkkhongsua.Checked = false;
                    chkduocsuatutao.Checked = false;
                    chkduocsua.Checked = true;
                }
                ////////////////////////////////
                if (nv.quyenxoa == null)
                {
                    chkkhongxoa.Checked = false;
                    chkduocxoatutao.Checked = false;
                    chkduocxoa.Checked = false;
                }
                if (nv.quyenxoa == 0)
                {
                    chkkhongxoa.Checked = true;
                    chkduocxoatutao.Checked = false;
                    chkduocxoa.Checked = false;
                }
                if (nv.quyenxoa == 1)
                {
                    chkkhongxoa.Checked = false;
                    chkduocxoatutao.Checked = true;
                    chkduocxoa.Checked = false;
                }
                if (nv.quyenxoa == 2)
                {
                    chkkhongxoa.Checked = false;
                    chkduocxoatutao.Checked = false;
                    chkduocxoa.Checked = true;
                }
            }
            IEnumerable<HT_menu> lmn = from c in context.HT_menus where c.cap.Equals(2) select c;
            HT_menu_nhanvien mnnv = new HT_menu_nhanvien();
            for (int i = 0; i < lmn.Count(); i++)
            {

                HT_menu item = lmn.ElementAt(i);
                this.grdata.Rows.Add();
                this.grdata.Rows[i].Cells["tenmenu"].Value = item.tenmenu;
                this.grdata.Rows[i].Cells["mamenu"].Value = item.mamenu;
                //
                mnnv.mamenu = item.mamenu;
                mnnv.manv = manv;
                IEnumerable<HT_menu_nhanvien> menunhanviens = from c in context.HT_menu_nhanviens where c.mamenu.Equals(mnnv.mamenu) where c.manv.Equals(mnnv.manv) select c;

                if (menunhanviens.Count() > 0)
                {
                    grdata.Rows[i].Cells["check"].Value = true;
                }


            }
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void savequyen()
        {

            string manv = this.lstdanhsach.SelectedValue.ToString();
            IEnumerable<DM_nhanvien> nhanviens = from c in context.DM_nhanviens where c.manv.Equals(manv) select c;
            if (nhanviens.Count() > 0)
            {
                for (int i = 0; i < nhanviens.Count(); i++)
                {
                    DM_nhanvien nhanvien = nhanviens.ElementAt(i);

                    if (chkduocthem.Checked == true)
                    {
                        nhanvien.quyenthem = 1;
                    }
                    if (chkkhongthem.Checked == true)
                    {
                        nhanvien.quyenthem = 0;
                    }
                    if (chkkhongsua.Checked == true)
                    {
                        nhanvien.quyensua = 0;
                    }
                    if (chkduocsuatutao.Checked == true)
                    {
                        nhanvien.quyensua = 1;
                    }
                    if (chkduocsua.Checked == true)
                    {
                        nhanvien.quyensua = 2;
                    }
                    if (chkkhongxoa.Checked == true)
                    {
                        nhanvien.quyenxoa = 0;
                    }
                    if (chkduocxoatutao.Checked == true)
                    {
                        nhanvien.quyenxoa = 1;
                    }
                    if (chkduocxoa.Checked == true)
                    {
                        nhanvien.quyenxoa = 2;
                    }


                }
                context.SubmitChanges();



            }


        }
        private void btSave_Click(object sender, EventArgs e)
        {
            try
            {
                savemenu();
                savequyen();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void savemenu()
        {

            try
            {

                string manv = this.lstdanhsach.SelectedValue.ToString();
                if (manv != "")
                {
                    ArrayList arrmamenu = new ArrayList();
                    for (int i = 0; i < this.grdata.RowCount; i++)
                    {
                        bool kq;
                        if (this.grdata.Rows[i].Cells["check"].Value != null)
                        {
                            kq = bool.Parse(this.grdata.Rows[i].Cells["check"].Value.ToString());
                        }
                        else
                        { kq = false; }
                        if (kq == true)
                        {
                            arrmamenu.Add(this.grdata.Rows[i].Cells["mamenu"].Value.ToString());

                        }
                    }
                    HT_menu_nhanvien mnnv;
                    string mamenu;
                    IEnumerable<HT_menu_nhanvien> menunvs = from c in context.HT_menu_nhanviens where c.manv.Equals(manv) select c;
                    context.HT_menu_nhanviens.DeleteAllOnSubmit(menunvs);
                    context.SubmitChanges();


                    for (int i = 0; i < arrmamenu.Count; i++)
                    {
                        mnnv = new HT_menu_nhanvien();
                        mnnv.manv = manv;
                        mamenu = arrmamenu[i].ToString();
                        mnnv.mamenu = mamenu;
                        IEnumerable<HT_menu_nhanvien> l = from c in context.HT_menu_nhanviens where c.mamenu.Equals(mamenu) where c.manv.Equals(manv) select c;
                        if (l.Count() < 1)
                        {
                            context.HT_menu_nhanviens.InsertOnSubmit(mnnv);
                        }

                    }
                    context.SubmitChanges();
                    MessageBox.Show("Lưu thành công.");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không lưu được.Lỗi: " + ex.Message);
            }
        }

        private void saveinfo()
        {
            string manv = this.lstdanhsach.SelectedValue.ToString();

            IEnumerable<DM_nhanvien> l = from c in context.DM_nhanviens where c.manv.Equals(manv) select c;


            // DM_nhanvien nv;

            if (manv != "")
            {


            }

        }






        private void chkduocthem_CheckedChanged(object sender, EventArgs e)
        {



        }

        private void chkkhongthem_CheckedChanged(object sender, EventArgs e)
        {


        }

        private void chkkhongthem_Click(object sender, EventArgs e)
        {
            //this.rdbkhongthem.Checked = true;
            if (this.chkkhongthem.Checked == true)
            {
                this.chkkhongthem.Checked = false;
                chkduocthem.Checked = true;
            }
            if (this.chkkhongthem.Checked == false)
            {
                this.chkkhongthem.Checked = true;
                this.chkduocthem.Checked = false;
            }
        }

        private void chkduocthem_Click(object sender, EventArgs e)
        {
            //this.rdbkhongthem.Checked = true;
            if (this.chkduocthem.Checked == true)
            {
                this.chkduocthem.Checked = false;
                chkkhongthem.Checked = true;
            }
            if (this.chkduocthem.Checked == false)
            {
                this.chkduocthem.Checked = true;
                this.chkkhongthem.Checked = false;
            }
        }

        private void chkduocsuatutao_Click(object sender, EventArgs e)
        {
            if (this.chkduocsuatutao.Checked == true)
            {

                this.chkduocsua.Checked = false;
                this.chkkhongsua.Checked = false;
                this.chkduocsuatutao.Checked = true;

            }
            if (this.chkduocsuatutao.Checked == false)
            {
                this.chkduocsua.Checked = false;
                this.chkkhongsua.Checked = false;
                this.chkduocsuatutao.Checked = true;

            }

        }

        private void chkkhongsua_Click(object sender, EventArgs e)
        {
            if (chkkhongsua.Checked == true)
            {
                this.chkduocsua.Checked = false;
                this.chkduocsuatutao.Checked = false;
                this.chkkhongsua.Checked = true;
            }
            if (chkkhongsua.Checked == false)
            {
                this.chkduocsua.Checked = false;
                this.chkduocsuatutao.Checked = false;
                this.chkkhongsua.Checked = true;
            }

        }
        private void chkduocsua_Click(object sender, EventArgs e)
        {
            if (this.chkduocsua.Checked == true)
            {
                this.chkkhongsua.Checked = false;
                this.chkduocsuatutao.Checked = false;
                this.chkduocsua.Checked = true;
            }
            if (this.chkduocsua.Checked == false)
            {
                this.chkkhongsua.Checked = false;
                this.chkduocsuatutao.Checked = false;
                this.chkduocsua.Checked = true;
            }
        }
        private void chkkhongxoa_Click(object sender, EventArgs e)
        {
            if (chkkhongxoa.Checked == true)
            {
                this.chkduocxoa.Checked = false;
                this.chkduocxoatutao.Checked = false;
                this.chkkhongxoa.Checked = true;
            }
            if (chkkhongxoa.Checked == false)
            {
                this.chkduocxoa.Checked = false;
                this.chkduocxoatutao.Checked = false;
                this.chkkhongxoa.Checked = true;
            }
        }

        private void chkduocxoatutao_Click(object sender, EventArgs e)
        {
            if (this.chkduocxoatutao.Checked == true)
            {
                this.chkduocxoa.Checked = false;
                this.chkkhongxoa.Checked = false;
                this.chkduocxoatutao.Checked = true;
            }
            if (this.chkduocxoatutao.Checked == false)
            {
                this.chkduocxoa.Checked = false;
                this.chkkhongxoa.Checked = false;
                this.chkduocxoatutao.Checked = true;

            }

        }

        private void chkduocxoa_Click(object sender, EventArgs e)
        {
            if (this.chkduocxoa.Checked == true)
            {
                this.chkkhongxoa.Checked = false;
                this.chkduocxoatutao.Checked = false;
                this.chkduocxoa.Checked = true;
            }
            if (this.chkduocxoa.Checked == false)
            {
                this.chkkhongxoa.Checked = false;
                this.chkduocxoatutao.Checked = false;
                this.chkduocxoa.Checked = true;
            }

        }

        private void grdata_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                if (grdata.Rows[e.RowIndex].Cells["check"].Value != null)
                {
                    grdata.Rows[e.RowIndex].Cells["check"].Value = null;
                }
            }
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }



    }
}
